package com.smartlib.cmnObject.util;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import com.smartlib.cmnObject.dialog.DialogOnClick;
import com.smartlib.cmnObject.dialog.FileUploadDialogFragment;
import com.smartlib.cmnObject.dialog.LoaddingDialogFragment;
import com.smartlib.cmnObject.dialog.TimePickerFragment;
import com.smartlib.cmnObject.dialog.TipDialogFragment;

/**
 * Created by admin on 16/6/30.
 * dialog util
 */
public class DialogUtil {

    public static String TAG = "dialog";


    /***
     * dismiss dialog
     */
    public static void dismiss(Context context) {
        if (context instanceof FragmentActivity) {
            FragmentActivity activity = (FragmentActivity) context;
            FragmentManager manager = activity.getFragmentManager();
            Fragment fragment = manager.findFragmentByTag(TAG);
            if (fragment != null) {
                FragmentTransaction fragmentTransaction = manager.beginTransaction();
                fragmentTransaction.remove(fragment);
                fragmentTransaction.commitAllowingStateLoss();
            }

        }
    }

    /***
     * 显示加载框
     *
     * @param context
     */
    public static void showLoadDiadlog(Context context) {
        LoaddingDialogFragment dialog = new LoaddingDialogFragment();
        if (context instanceof FragmentActivity) {
            FragmentActivity activity = (FragmentActivity) context;
            FragmentManager manager = activity.getFragmentManager();
            FragmentTransaction fragmentTransaction = manager
                    .beginTransaction();
            fragmentTransaction.add(dialog, TAG);
            fragmentTransaction.commitAllowingStateLoss();
        }

    }

    /***
     * 上传的dialog
     * @param context
     * @param onClickCarmer
     * @param onClickAlbum
     */
    public static void showUploadFile(Context context, DialogOnClick<String> onClickCarmer, DialogOnClick<String> onClickAlbum) {
        FileUploadDialogFragment dialog = new FileUploadDialogFragment();
        dialog.setOnClick(onClickCarmer, onClickAlbum);
        if (context instanceof FragmentActivity) {
            FragmentActivity activity = (FragmentActivity) context;
            FragmentManager manager = activity.getFragmentManager();
            FragmentTransaction fragmentTransaction = manager
                    .beginTransaction();
            fragmentTransaction.add(dialog, TAG);
            fragmentTransaction.commitAllowingStateLoss();
        }
    }
    /***
     * 上传的dialog
     * @param context
     * @param onClickCarmer
     * @param onClickAlbum
     */
    public static void showUploadFile(Context context,String carmer,String album, DialogOnClick<String> onClickCarmer, DialogOnClick<String> onClickAlbum) {
        FileUploadDialogFragment dialog = new FileUploadDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putString("carmer",carmer);
        bundle.putString("album",album);
        dialog.setArguments(bundle);
        dialog.setOnClick(onClickCarmer, onClickAlbum);
        if (context instanceof FragmentActivity) {
            FragmentActivity activity = (FragmentActivity) context;
            FragmentManager manager = activity.getFragmentManager();
            FragmentTransaction fragmentTransaction = manager
                    .beginTransaction();
            fragmentTransaction.add(dialog, TAG);
            fragmentTransaction.commitAllowingStateLoss();
        }
    }

    public static void showSimpleAlertDialog(Context context, DialogOnClick onClick, String msg) {
        TipDialogFragment dialog = new TipDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putString("msg", msg);
        dialog.setArguments(bundle);
        if (onClick != null) {
            dialog.setOnClick(onClick);
        }
        if (context instanceof FragmentActivity) {
            FragmentActivity activity = (FragmentActivity) context;
            FragmentManager manager = activity.getFragmentManager();
            FragmentTransaction fragmentTransaction = manager
                    .beginTransaction();
            fragmentTransaction.add(dialog, TAG);
            fragmentTransaction.commitAllowingStateLoss();
        }
    }
    public static void showSimpleAlertDialog(Context context, String msg) {
        showSimpleAlertDialog(context, null, msg);
    }

    public static void showDatePick(Context context,DialogOnClick<String> onClick)
    {
        TimePickerFragment fragment = new TimePickerFragment();
        fragment.setOnClick(onClick);
        if (context instanceof FragmentActivity) {
            FragmentActivity activity = (FragmentActivity) context;
            FragmentManager manager = activity.getFragmentManager();
            FragmentTransaction fragmentTransaction = manager
                    .beginTransaction();
            fragmentTransaction.add(fragment, TAG);
            fragmentTransaction.commitAllowingStateLoss();
        }
    }

}
